package control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.Database;
import model.Pallet;
import gui.Gui;

public class DatabaseControl { //A class
	private Gui view;
	private Database db;
	
	/**
	 * Creates the controller between the main gui and the database
	 * @param gui The gui to be controlled
	 * @param db the database model used
	 */
	public DatabaseControl(Gui view, Database db){
		this.view = view;
		this.db = db;
		
		//add listeners to the buttons
		view.addSearchListener(new SearchListener());
		view.addBlockListener(new BlockPalletListener());
		view.addCreateListener(new CreatePalletListener());
		view.addSearchSelectionListener(new SearchSelectionListener());
		view.addUnBlockedListener(new UnBlockPalletListener());
		view.addStartItemListener(new StartItemListener(), new StartItemListener());
		view.addEndItemListener(new EndItemListener(), new EndItemListener());
//		view.updateOrderComboBox(db.getOrders());
		
	}
	//Listener classes for all the buttons
	
	/**
	 * The search buttons listener
	 */
	private class SearchListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			if(!dbConnected()){
				System.err.println("Database is not connected");
				return;
			}
			ArrayList<Pallet> dbElements = db.searchDb(view.getSearchComboboxValue(),view.getSearchField(), view.searchBlockedPallets());
			if(dbElements != null) {
				view.updateSearchList(dbElements);
			}else{
				dbElements = new ArrayList<Pallet>();
				dbElements.add(new Pallet("Your search didn't result in any hits in the databace","","","","",""));
				view.updateSearchList(dbElements);
			}
		}
	}
		
	/**
	 * The search fields listener
	 * @author marcus
	 *
	 */
	private class SearchSelectionListener implements ListSelectionListener{
		public void valueChanged(ListSelectionEvent arg0) {
			if(!dbConnected()){
				return;
			}
			String palletId = view.getSelectedPallet();
			if(palletId == null) return;
			int id;
			try{
			id = Integer.parseInt(palletId);
			}catch(NumberFormatException e){
				return;
			}
			Pallet pallet = db.getPallet(id);
			if(pallet == null){
				view.setSearchLables(new Pallet("Couldn't find a pallet that matches" +
						" your description, please try again!", "", "", "", "", ""));
			} else {
				view.setSearchLables(pallet);
			}
		}
	}
	
	/**
	 * The blocked pallet button listener
	 * @author marcus
	 *
	 */
	private class BlockPalletListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(!dbConnected()){
				return;
			}
			String palletId = view.getSelectedPallet();
			boolean success;
			if(palletId != null){
				success = db.blockPallet(Integer.parseInt(palletId), view.getFromDate(), view.getToDate());
			}else{
				view.setSearchLables(new Pallet("Couldn't find a pallet that matches" +
						" your description, please try again!", "", "", "", "", ""));
				success = false;
			}
			if(!success){
				view.showFailure("block");
			}
		}
	}
	
	/**
	 * the Create pallet listener
	 * @author marcus
	 *
	 */
	private class CreatePalletListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			if(!dbConnected()){
				return;
			}
			Pallet dbElement = view.getCreatedPallet();
			boolean success = db.addPallet(dbElement);
			if(!success){
				view.showFailure("create");
			}
			Pallet pallet = db.getLatestPallet();
			view.setCreateLables(pallet);
		}
	}
	
	private boolean dbConnected(){
		return db.isConnected();		
	}
	
	
	
	private class UnBlockPalletListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			if(!dbConnected()){
				return;
			}
			String palletId = view.getSelectedPallet();
			boolean success;
			if(palletId != null){
				success = db.unBlockPallet(Integer.parseInt(palletId), view.getFromDate(), view.getToDate());
			}else{
				view.setSearchLables(new Pallet("Couldn't find a pallet that matches" +
						" your description, please try again!", "", "", "", "", ""));
				success = false;
			}
			if(!success){
				view.showFailure("block");
			}
		}
	}
	
	/**
	 * The year and months fields listener for the start date of interval
	 * @author Adam
	 *
	 */
	private class StartItemListener implements ItemListener{
		@Override
		public void itemStateChanged(ItemEvent e) {
			view.updateStartDayComboBox();
		}
	}
	
	/**
	  * The year and months fields listener for the end date of interval
	 * @author Adam
	 *
	 */
	private class EndItemListener implements ItemListener{
		@Override
		public void itemStateChanged(ItemEvent e) {
			view.updateEndDayComboBox();
		}
	}
}
